package com.lp.sys.function;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.context.BaseSpringContextSupport;

import com.lp.business.function.BaseBusinessUtil;
import com.lp.business.model.Menu;
import com.lp.business.model.Role;
import com.lp.business.model.User;
import com.lp.business.service.MenuService;
import com.lp.business.service.RoleService;
import com.lp.business.service.UserService;
import com.lp.sys.conf.Conf;

/**
 * 业务工具方法
 * 
 * @author lipeng
 * 
 */
public class BusinessUtil extends BaseBusinessUtil {
	
	/**
	 * 
	 * @param url
	 * @return String 全路径
	 */
	public static String getFullPath(String url) {
		if(V.isEmpty(url)){
			return "";
		}
		if(url.startsWith("http")){
			return url;
		}
		return Conf.IMG_BASE_RES+url;
	}
	
	/**
	 * 【菜单业务】通过主键ID参数返回菜单中文名称字符串
	 * 
	 * @param menuId
	 *            菜单ID
	 * @return String 返回菜单中文名称字符串
	 */
	public static String getMemuUpStrName(String menuId) {
		MenuService menuService = (MenuService) BaseSpringContextSupport.getApplicationContext().getBean("menuService");
		Menu m = new Menu();
		m.setId(menuId);
		menuService.load(m);
		return m.getMenuName();
	}

	/**
	 * 【菜单业务】通过整型参数返回菜单等级中文字符串<br/>
	 * 例如：${common.getMemuLevelStrName(vo.level)}<br/>
	 * 
	 * @param level
	 *            菜单等级
	 * @return String 返回等级对应的字符串
	 */
	public static String getMemuLevelStrName(Integer level) {
		String strName = "";
		switch (level) {
		case 0:
			strName = "父级菜单";
			break;
		case 1:
			strName = "&nbsp;&nbsp;子菜单";
			break;
		case 2:
			strName = "&nbsp;&nbsp;&nbsp;&nbsp;子菜单";
			break;
		default:
			strName = "<font color='red'>菜单等级参数非法！</font>";
		}
		return strName;
	}

	/**
	 * 【角色业务】获取父级角色ID所属下拉列表HTML字符串<br/>
	 * 例如：${common.getSelectByMenu("40288092394735dd01394735f3x30001",0)}<br/>
	 * 
	 * @param tagName
	 *            select标签 name
	 * @param selectRoleId
	 *            角色父级ID
	 * @param level
	 *            角色等级
	 * @return String 所属菜单下拉列表HTML字符串
	 */
	public static String getSelectByRole(String tagName, String selectRoleId, int level) {
		selectRoleId = selectRoleId == null ? "" : selectRoleId;
		String levelCondition = "";
		if (level == 0) {
			levelCondition = "r.level=0";
		} else if (level == 1) {
			levelCondition = "r.level=1";
		}
		RoleService roleService = (RoleService) BaseSpringContextSupport.getApplicationContext().getBean("roleService");
		List<Role> roles = roleService
				.queryHQL("from Role r where " + levelCondition + " and r.status='AVAILABLE' order by r.id");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<select name='" + tagName
				+ "' class='form-control' style='width: 100px;'><option value=''>请选择</option>");
		for (Role r : roles) {
			stringBuffer.append(
					"<option value='" + r.getId() + "' " + (selectRoleId.equals(r.getId()) ? "selected='selected'" : "")
							+ ">" + r.getRoleName() + "</option>");
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}

	/**
	 * 【菜单业务】根据父级菜单ID获取所属菜单下拉列表HTML字符串<br/>
	 * 例如：${common.getSelectByMenuUpId("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param upId
	 *            菜单父级ID
	 * @return String 所属菜单下拉列表HTML字符串
	 */
	public static String getSelectByMenuUpId(String menuId) {
		menuId = menuId == null ? "" : menuId;
		MenuService menuService = (MenuService) BaseSpringContextSupport.getApplicationContext().getBean("menuService");
		List<Menu> menus = menuService.queryHQL("from Menu m where m.level=0 order by m.sort");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<select name='menuId' class='form-control required'><option value=''>请选择</option>");
		for (Menu m : menus) {
			stringBuffer.append("<option value='" + m.getId() + "' "
					+ (menuId.equals(m.getId()) ? "selected='selected'" : "") + ">" + m.getMenuName() + "</option>");
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}

	/**
	 * 【菜单业务】获取所有菜单并按顺序组织出HTML字符串<br/>
	 * 例如：${common.getSelectByMenu("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param tagName
	 *            下拉框的name属性值
	 * @param selectMenuId
	 *            被选中的菜单ID
	 * @return String 父菜单下拉列表
	 */
	public static String getSelectByMenu(String tagName, String selectMenuId) {
		selectMenuId = selectMenuId == null ? "" : selectMenuId;
		MenuService menuService = (MenuService) BaseSpringContextSupport.getApplicationContext().getBean("menuService");
		List<Menu> menus = menuService.queryHQL("from Menu m where m.level=0 and m.status='AVAILABLE' order by m.sort");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<select name='" + tagName + "' class='form-control'><option value=''>请选择</option>");
		for (Menu m : menus) {
			stringBuffer.append(
					"<option value='" + m.getId() + "' " + (selectMenuId.equals(m.getId()) ? "selected='selected'" : "")
							+ ">" + m.getMenuName() + "</option>");
		}
		stringBuffer.append("</select>");
		L.i("stringBuffer.toString()------->" + stringBuffer.toString());
		return stringBuffer.toString();
	}

	/**
	 * 【菜单业务】获取所有菜单并按顺序组织出HTML字符串<br/>
	 * 例如：${common.getSelectByMenuAll("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param tagName
	 *            下拉框的name属性值
	 * @param menuId
	 *            被选中的菜单ID
	 * @return String 所属菜单下拉列表HTML字符串
	 */
	public static String getSelectByMenuAll(String tagName, String menuId) {
		menuId = menuId == null ? "" : menuId;
		MenuService menuService = (MenuService) BaseSpringContextSupport.getApplicationContext().getBean("menuService");
		List<Menu> menus = menuService.queryHQL("from Menu m where m.level=0 and m.status='AVAILABLE' order by m.sort");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
				.append("<select name='" + tagName + "' class='form-control required' ><option value=''>请选择</option>");
		for (Menu m : menus) {
			stringBuffer.append("<option disabled='true'>" + m.getMenuName() + "</option>");
			List<Menu> submenus = menuService
					.queryHQL("from Menu s where s.level=1 and s.status='AVAILABLE' and s.menuId='" + m.getId()
							+ "' order by s.sort");
			for (Menu s : submenus) {
				stringBuffer.append(
						"<option value='" + s.getId() + "' " + (menuId.equals(s.getId()) ? "selected='selected'" : "")
								+ ">&nbsp;&nbsp;&nbsp;|--" + s.getMenuName() + "</option>");
			}
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}

	/**
	 * 【用户业务】根据用户ID获取下拉列表HTML字符串<br/>
	 * 例如：${common.getSelectByUserId("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param userId
	 *            用户ID
	 * 
	 * @return String 所属用户下拉列表HTML字符串
	 */
	public static String getSelectByUserId(String userId) {
		userId = userId == null ? "" : userId;
		UserService userService = (UserService) BaseSpringContextSupport.getApplicationContext().getBean("userService");
		List<User> users = userService.queryHQL("from User u order by u.userName");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
				.append("<select name='user.id' class='required' style='width: 100px;'><option value=''>请选择</option>");
		for (User u : users) {
			stringBuffer.append("<option value='" + u.getId() + "' "
					+ (userId.equals(u.getId()) ? "selected='selected'" : "") + ">" + u.getUserName() + "</option>");
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}

	/**
	 * 【记录状态】根据不通的记录状态进行相应的操作<br/>
	 * 例如：${common.getRecordStatusOperate(vo.status,vo.id,basePath,navTabId,
	 * 'dialogAjaxEdit')}<br/>
	 * 
	 * @param recordStatus
	 *            记录状态 (必须)
	 * @param id
	 *            记录ID (必须)
	 * @param basePath
	 *            基本请求路径 (必须)
	 * @param navTabId
	 *            回调刷新的导航ID (必须)
	 * @param callback
	 *            回调的类型 (必须)
	 * @return String 返回已经加上操作的状态信息
	 */
	public static String getRecordStatusOperate(String recordStatus, String id, String basePath, String navTabId,
			String callback) {
		if (recordStatus.equals(RecordStatus.AVAILABLE.toString())) {
			return "<a class='btn btn-outline btn-default btn-xs' href='" + basePath + "/admin/" + navTabId
					+ "/updateStatus.do?id=" + id + "&status=" + RecordStatus.FORBID + "' option='dialogCheck'>"
					+ RecordStatus.FORBID.getDesc() + "</a>";
		}

		if (recordStatus.equals(RecordStatus.FORBID.toString())) {
			return "<a class='btn btn-outline btn-default btn-xs' href='" + basePath + "/admin/" + navTabId
					+ "/updateStatus.do?id=" + id + "&status=" + RecordStatus.AVAILABLE + "' option='dialogCheck'>"
					+ RecordStatus.AVAILABLE.getDesc() + "</a>";
		}

		if (recordStatus.equals(RecordStatus.DELETE.toString())) {
			return "<a class='btn btn-outline btn-default btn-xs' href='" + basePath + "/admin/" + navTabId
					+ "/updateStatus.do?id=" + id + "&status=" + RecordStatus.AVAILABLE
					+ "' option='dialogCheck'>撤销</a>";
		}

		return "";
	}

	/**
	 * 【记录状态】通过记录状态显示不通的提示颜色<br/>
	 * 例如：${common.getRecordStatus(vo.status)}<br/>
	 * 
	 * @param recordStatus
	 *            记录状态
	 * @return String 返回已经加上颜色的状态信息
	 */
	public static String getRecordStatus(String recordStatus) {
		if (recordStatus.equals(RecordStatus.AVAILABLE.toString())) {
			recordStatus = "<span style='color:green;line-height:21px;'>" + RecordStatus.AVAILABLE.getDesc()
					+ "</span>";
			return recordStatus;
		}

		if (recordStatus.equals(RecordStatus.DELETE.toString())) {
			recordStatus = "<span style='color:red;line-height:21px;'>" + RecordStatus.DELETE.getDesc() + "</span>";
			return recordStatus;
		}

		if (recordStatus.equals(RecordStatus.FORBID.toString())) {
			recordStatus = "<span style='color:gray;line-height:21px;'>" + RecordStatus.FORBID.getDesc() + "</span>";
			return recordStatus;
		}

		return "";
	}
	
	/**
	 * 【地址处理】根据现有连接判断是否已经带参数，如果已经带参数则导航参数&方式添加如果没有参数则?方式添加
	 * 
	 * @param basePath
	 *            基本路径
	 * @param url
	 *            请求的地址
	 * @param nav
	 *            导航参数
	 * @param type
	 *            类型
	 * @return 返回解析拼接后的URL地址链接
	 */
	public static String getResolveNavTabIdUrl(String basePath, String url, String nav, String type) {
		if (StringUtils.equals(type, "EXTERNAL")) {
			return url;
		}
		url = url.indexOf("?") > 0 ? url + "&navTabId=" + nav : url + "?navTabId=" + nav;
		return basePath + url;

	}
	
	/**
	 * 【角色业务】根据父级角色ID获取所属角色下拉列表HTML字符串<br/>
	 * 例如：${common.getSelectByMenuUpId("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param upId
	 *            角色父级ID
	 * @return String 所属菜单下拉列表HTML字符串
	 */
	public static String getSelectByRoleUpId(String roleId) {
		roleId = roleId == null ? "" : roleId;
		RoleService roleService = (RoleService) BaseSpringContextSupport.getApplicationContext().getBean("roleService");
		List<Role> roles = roleService.queryHQL("from Role r where r.level=0 order by r.id");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<select name='roleId' class='required' style='width: 100px;'><option value=''>请选择</option>");
		for (Role r : roles) {
			stringBuffer.append("<option value='" + r.getId() + "' "
					+ (roleId.equals(r.getId()) ? "selected='selected'" : "") + ">" + r.getRoleName() + "</option>");
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}

	/**
	 * 【角色业务】通过整型参数返回角色等级中文字符串<br/>
	 * 例如：${common.getRoleLevelStrName(vo.level)}<br/>
	 * 
	 * @param level
	 *            角色等级
	 * @return String 返回等级对应的字符串
	 */
	public static String getRoleLevelStrName(Integer level) {
		String strName = "";
		switch (level) {
		case 0:
			strName = "父级角色";
			break;
		case 1:
			strName = "&nbsp;&nbsp;子角色";
			break;
		case 2:
			strName = "&nbsp;&nbsp;&nbsp;&nbsp;子角色";
			break;
		default:
			strName = "<font color='red'>角色等级参数非法！</font>";
		}
		return strName;
	}

	/**
	 * 【菜单业务】根据父级菜单ID获取所属菜单下拉列表HTML字符串<br/>
	 * 例如：${common.getSelectByMenuUpId("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param upId
	 *            菜单父级ID
	 * @return String 所属菜单下拉列表HTML字符串
	 */
	public static String getSelectByMenuUpIdAdmin(String menuId) {
		menuId = menuId == null ? "" : menuId;
		MenuService menuService = (MenuService) BaseSpringContextSupport.getApplicationContext().getBean("menuService");
		List<Menu> menus = menuService.queryHQL("from Menu m where m.level=0 order by m.sort");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<select name='menuId' class='required' style='width: 100px;'><option value=''>请选择</option>");
		for (Menu m : menus) {
			stringBuffer.append("<option value='" + m.getId() + "' "
					+ (menuId.equals(m.getId()) ? "selected='selected'" : "") + ">" + m.getMenuName() + "</option>");
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}

	/**
	 * 【菜单业务】获取所有菜单并按顺序组织出HTML字符串<br/>
	 * 例如：${common.getSelectByMenuAll("40288092394735dd01394735f3x30001")}<br/>
	 * 
	 * @param tagName
	 *            下拉框的name属性值
	 * @param menuId
	 *            被选中的菜单ID
	 * @return String 所属菜单下拉列表HTML字符串
	 */
	public static String getSelectByMenuAllAdmin(String tagName, String menuId) {
		menuId = menuId == null ? "" : menuId;
		MenuService menuService = (MenuService) BaseSpringContextSupport.getApplicationContext().getBean("menuService");
		List<Menu> menus = menuService.queryHQL("from Menu m where m.level=0 and m.status='AVAILABLE' order by m.sort");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(
				"<select name='" + tagName + "' class='required' style='width: 100px;'><option value=''>请选择</option>");
		for (Menu m : menus) {
			stringBuffer.append("<option>" + m.getMenuName() + "</option>");
			List<Menu> submenus = menuService
					.queryHQL("from Menu s where s.level=1 and s.status='AVAILABLE' and s.menuId='" + m.getId()
							+ "' order by s.sort");
			for (Menu s : submenus) {
				stringBuffer.append(
						"<option value='" + s.getId() + "' " + (menuId.equals(s.getId()) ? "selected='selected'" : "")
								+ ">&nbsp;&nbsp;&nbsp;|--" + s.getMenuName() + "</option>");
			}
		}
		stringBuffer.append("</select>");
		return stringBuffer.toString();
	}
	
}
